package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.user;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.service.OrgFeignService;
import com.alibaba.algo.cooperation.feign.service.UserFeignService;
import com.alibaba.algo.cooperation.feign.vo.req.GetOrgDetailReqVo;
import com.alibaba.algo.cooperation.feign.vo.req.GetUserOrgsReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectOrgUserReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.FeignOrgDetailResponse;
import com.alibaba.algo.cooperation.feign.vo.res.OrgUserInfoResVO;
import com.alibaba.algo.cooperation.feign.vo.res.UserOrgInfoResVO;
import com.alibaba.algo.cooperation.usercomponent.service.user.SysOrgService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserOrgService;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.StringUtils;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.OrgInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.UserInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description 用户所属组织服务实现方法
 * @author liujinjian
 * @date 2021-11-01
 * @version 1.0
 */
@Service
public class UserOrgServiceImpl implements UserOrgService {

    @Autowired
    private OrgFeignService orgFeignService;

    @Autowired
    private SysOrgService sysOrgService;

    @Autowired
    private UserFeignService userFeignService;



    /**
     * 通过组织ID获取组织下的用户列表
     * @param orgCode
     * @return
     */
    @Override
    public List<UserInfo> getUserListByOrgId(String orgCode) {
        List<UserInfo> userInfoList = new ArrayList<UserInfo>();
        SelectOrgUserReqVO reqVo = new SelectOrgUserReqVO();
        reqVo.setOrgId(Long.valueOf(orgCode));
        BaseResult<List<OrgUserInfoResVO>> dataList = orgFeignService.qryUserListByOrgId(reqVo);
        for (OrgUserInfoResVO vo : dataList.getData()) {
            UserInfo entity = new UserInfo();
            entity.setUserName(vo.getUserName());
            entity.setUserCode(vo.getUserId().toString());
            userInfoList.add(entity);
        }
        return userInfoList;
    }

    @Override
    public List<OrgInfo> getOrgInfoByUserId(String userId) {
        GetUserOrgsReqVO reqVO = new GetUserOrgsReqVO();
        reqVO.setUserId(Long.parseLong(userId));
        BaseResult<List<UserOrgInfoResVO>> userOrgInfoResVos = userFeignService.getOrgsByUser(reqVO);
        if(Objects.isNull(userOrgInfoResVos)) {
            return null;
        }
        List<UserOrgInfoResVO> userOrgInfoResVoList = userOrgInfoResVos.getData();
        if(CollectionUtils.isEmpty(userOrgInfoResVoList)) {
            return null;
        }
        List<OrgInfo> orgInfoList = new ArrayList<>();
        for (UserOrgInfoResVO userOrgInfoResVO : userOrgInfoResVoList) {
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setOrgCode(String.valueOf(userOrgInfoResVO.getOrgId()));
            orgInfo.setOrgName(userOrgInfoResVO.getOrgName());
            orgInfoList.add(orgInfo);
        }
        return orgInfoList;
    }

    @Override
    public OrgInfo getOrgByCode(String code) {
        List<Long> orgCodeList = new ArrayList<>();
        orgCodeList.add(Long.valueOf(code));
        BaseResult<Map<Long, String>> resultMap = orgFeignService.qryOrgNameMap(orgCodeList);
        if (Objects.nonNull(resultMap) && Objects.nonNull(resultMap.getData())){
            Map<Long, String> result = resultMap.getData();
            String name = result.get(Long.valueOf(code));
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setOrgCode(code);
            orgInfo.setOrgName(name);
            return orgInfo;
        }
        return null;
    }

    /**
     * 通过组织ID集合获取组织下的用户列表支持租户
     *
     * @param orgIdList 组织ID集合
     * @return
     */
    @Override
    public List<UserInfo> getUserListByOrg(List<String> orgIdList) {
        SelectOrgUserReqVO reqVO = new SelectOrgUserReqVO();
        reqVO.setOrgIdList(orgIdList);
        BaseResult<List<OrgUserInfoResVO>> orgUserInfoResVo = orgFeignService.qryUserListByOrg(reqVO);
        if(Objects.isNull(orgUserInfoResVo)) {
            return Collections.emptyList();
        }
        List<OrgUserInfoResVO> orgUserInfoResVoList = orgUserInfoResVo.getData();
        if(CollectionUtils.isEmpty(orgUserInfoResVoList)) {
            return Collections.emptyList();
        }
        // 转换成用户集合
        List<UserInfo> userInfoList = new ArrayList<UserInfo>();
        UserInfo userInfo;
        for (OrgUserInfoResVO vo : orgUserInfoResVoList) {
            userInfo = new UserInfo();
            userInfo.setUserName(vo.getUserName());
            userInfo.setUserCode(String.valueOf(vo.getUserId()));
            userInfoList.add(userInfo);
        }
        return userInfoList;
    }

    /**
     * 根据组织ID获取组织对象信息
     *
     * @param orgId
     * @return
     */
    @Override
    public OrgInfo getOrgDetail(String orgId) {
        if(StringUtils.isBlank(orgId)) {
            return null;
        }
        GetOrgDetailReqVo orgDetailReqVo=new GetOrgDetailReqVo();
        orgDetailReqVo.setOrgId(Long.valueOf(orgId));
        BaseResult<FeignOrgDetailResponse> orgDetail = orgFeignService.getOrgDetail(orgDetailReqVo);
        FeignOrgDetailResponse feignOrgDetailResponse = orgDetail.getData();
        if(feignOrgDetailResponse!=null){
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setCode(feignOrgDetailResponse.getOrgCode());
            orgInfo.setOrgName(feignOrgDetailResponse.getOrgName());
            return orgInfo;
        }
        return null;
    }

    @Override
    public OrgInfo getOrgDetailByOrgCode(String orgCode) {
        if(StringUtils.isBlank(orgCode)) {
            return null;
        }
        GetOrgDetailReqVo orgDetailReqVo=new GetOrgDetailReqVo();
        orgDetailReqVo.setOrgCode(orgCode);
        BaseResult<FeignOrgDetailResponse> orgDetail = orgFeignService.getOrgDetail(orgDetailReqVo);
        FeignOrgDetailResponse feignOrgDetailResponse = orgDetail.getData();
        if(feignOrgDetailResponse!=null){
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setOrgCode(String.valueOf(feignOrgDetailResponse.getId()));
            orgInfo.setCode(feignOrgDetailResponse.getOrgCode());
            orgInfo.setOrgName(feignOrgDetailResponse.getOrgName());
            return orgInfo;
        }
        return null;
    }
}
